<template>
  <div>
    <el-dialog
      width="85%"
      :title="$t('详情')"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel">
      <el-form :model="queryParams" ref="queryForm" size="small" inline>
        <el-form-item prop="orderNo">
          <el-input
            :placeholder="$t('订单号')"
            v-model="queryParams.orderNo"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <el-form-item prop="shopName">
          <el-input
            :placeholder="$t('店铺名称')"
            v-model="queryParams.shopName"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <!-- 租户名称 -->
        <el-form-item prop="distrName">
          <el-input
            :placeholder="$t('租户名称')"
            v-model="queryParams.distrName"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <!-- 姓 -->
        <el-form-item prop="familyName">
          <el-input
            :placeholder="$t('姓')"
            v-model="queryParams.familyName"
            clearable
            @keyup.enter.native="getList" />
        </el-form-item>
        <!-- 购买时间 -->
        <el-form-item prop="purchaseDate">
          <el-date-picker
            :placeholder="$t('购买时间')"
            style="width: 300px"
            v-model="queryParams.time"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">
            <!-- 搜索 -->
            {{ $t('搜索') }}
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            <!-- 重置 -->
            {{ $t('重置') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-row
        style="
          text-align: right;
          margin-bottom: 10px;
          display: flex;
          align-items: center;
          justify-content: flex-end;
        ">
        <div style="margin: 0 15px">
          同步状态:
          <span v-if="formData.isSync == 1" style="color: green">
            {{ $t('已同步') }}
          </span>
          <span v-if="formData.isSync == 0" style="color: red">
            {{ $t('未同步') }}
          </span>
        </div>
        <el-button
          v-hasPermi="['distribution:orderMercari:syncData']"
          type="primary"
          @click="handleSync"
          :disabled="formData.isSync == 1"
          size="mini">
          {{ $t('同步') }}
        </el-button>
      </el-row>

      <el-table :height="calTableHeight() - 200" ref="mianRef" v-loading="loading" :data="tableData">
        <!-- 订单号 -->
        <el-table-column show-overflow-tooltip label="订单号" width="150" align="center" prop="orderNo" />
        <!-- 店铺名称 -->
        <el-table-column show-overflow-tooltip label="店铺名称" align="center" prop="shopName" />
        <!-- 租户名称 -->
        <el-table-column show-overflow-tooltip label="租户名称" align="center" prop="distrName" />
        <!-- 商品名称 -->
        <el-table-column show-overflow-tooltip label="商品名称" width="250" align="center" prop="itemName">
          <template slot-scope="{ row }">
            <el-input :autosize="{ minRows: 1, maxRows: 2 }" type="textarea" v-model="row.itemName" />
          </template>
        </el-table-column>
        <!-- 价格 -->
        <el-table-column show-overflow-tooltip label="价格" align="center" prop="price" />

        <!-- 支付金额 -->
        <!-- <el-table-column
          show-overflow-tooltip
          label="支付金额"
          align="center"
          prop="paymentFee"
        /> -->
        <!-- 支付价格 -->
        <!-- <el-table-column
          show-overflow-tooltip
          label="支付价格"
          align="center"
          prop="paidPrice"
        /> -->
        <!-- 购买时间 -->
        <el-table-column show-overflow-tooltip label="购买时间" align="center" prop="purchaseDate" />
        <!-- 订单状态 -->
        <el-table-column show-overflow-tooltip label="订单状态" align="center" prop="status" />

        <!-- 邮编 -->
        <el-table-column show-overflow-tooltip label="邮编" align="center">
          <template slot-scope="{ row }">
            {{ `〒${row.zipCode1 || ''}-${row.zipCode2 || ''}` }}
          </template>
        </el-table-column>
        <!-- 姓名 -->
        <el-table-column show-overflow-tooltip label="姓名" align="center">
          <template slot-scope="{ row }">
            {{ `${row.familyName || ''} ${row.familyNameKana || ''}` }}
          </template>
        </el-table-column>
        <!-- 地址 -->
        <el-table-column show-overflow-tooltip label="地址" align="center" prop="purchaseDate">
          <template slot-scope="{ row }">
            {{ handleddress(row) }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column :label="$t('操作')" align="center" fixed="right">
          <template slot-scope="{ row }">
            <el-button
              v-hasPermi="['distribution:orderMercari:updateOrderMercariIndividual']"
              @click="handleEdit(row)"
              type="text">
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <ErrList :show.sync="errList.show" :formData="errList.formData" />
  </div>
</template>

<script>
import {
  getList,
  updateOrderMercariIndividual,
  syncData,
  checkSku,
} from '@/api/orderManage/mercariPersonalSync'
import ErrList from './errList'
export default {
  activated() {
    this.$nextTick(() => this.$refs['mianRef']?.doLayout())
  },
  components: { ErrList },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      errList: {
        show: false,
        formData: {},
      },
      showSearch: true,
      queryParams: {
        time: [],
      },
      form: {},
      // 表单校验
      rules: {
        distrName: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      tableData: [],
      // 遮罩层
      loading: true,
      total: 0,
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.queryParams.time = []
        this.getList()
      }
    },
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleSync() {
      this.$confirm(this.$t('确认同步?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const row = this.formData
        checkSku(row.recordsId).then((res) => {
          const { code, data } = res
          if (code === 200 && !data) {
            syncData({ distrId: row.distrId, recordsId: row.recordsId }).then((res) => {
              const { code, data, msg } = res
              if (code === 200) {
                this.$message({ type: 'success', message: msg })
                this.getList()
              }
            })
          } else {
            this.errList.show = true
            this.errList.formData = data
          }
        })
      })
    },
    handleEdit(row) {
      const data = {
        id: row.id,
        itemName: row.itemName,
      }
      updateOrderMercariIndividual(data).then((res) => {
        const { code, data, msg } = res
        if (code === 200) {
          this.$message({ type: 'success', message: msg })
          this.getList()
        }
      })
    },
    handleddress(row) {
      return (row.prefecture || '') + (row.city || '') + (row.address1 || '') + (row.address2 || '')
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = ''))
      this.queryParams.pageSize = 10
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 查询供应厂家列表 */
    getList() {
      /****************** 处理查询条件 *********************/
      const selectDate = this.queryParams.time || []
      const createTimeStart = selectDate[0] || ''
      const createTimeEnd = selectDate[1] || ''
      const data = {
        ...this.queryParams,
        createTimeStart,
        createTimeEnd,
      }
      delete data.time
      /**************** 处理查询条件 end *******************/
      this.loading = true
      getList({ ...data, recordsId: this.formData.recordsId }).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    handleSubmit() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss"></style>
